package demo.gui;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.UndoManager;
import java.util.Date;
import java.text.SimpleDateFormat;

public class MyNote extends JFrame {
	/**
	 *
	 */
	private static final long serialVersionUID = -1104922068358761584L;
	// �˵�
	JMenuBar menub = new JMenuBar();
	// ��ʾ���ı��Ķ�������
	JTextArea text = new JTextArea();
	JMenu files = new JMenu("�ļ�(F)");
	JMenu edit = new JMenu("�༭(E)");
	JMenu formats = new JMenu("��ʽ(O)");
	JMenu help = new JMenu("����(H)");
	JMenuItem newFile = new JMenuItem("�½�(N)");
	JMenuItem open = new JMenuItem("��(O)");
	JMenuItem save = new JMenuItem("����(S)");
	JMenuItem saveAs = new JMenuItem("���Ϊ(A)");
	JMenuItem exit = new JMenuItem("�˳�(X)");
	JMenuItem undo = new JMenuItem("����(U)");
	JMenuItem cut = new JMenuItem("����(T)");
	JMenuItem copy = new JMenuItem("����(C)");
	JMenuItem paste = new JMenuItem("ճ��(P)");
	JMenuItem selectAll = new JMenuItem("ȫѡ(A)");
	JMenuItem timeDate = new JMenuItem("ʱ��/����(D)");
	JCheckBoxMenuItem lineWrap = new JCheckBoxMenuItem("�Զ�����(M)");
	JMenuItem fonts = new JMenuItem("����");
	JMenuItem about = new JMenuItem("���ڼ��±�(A)");
	JFrame th = this;
	String name;
	String openedPath = null;
	boolean opened = false;
	boolean reworked = false;
	UndoManager undoManager = new UndoManager();

	// ��ʼ������
	public MyNote(String name) {
		super(name);
		this.name = name;
		int x, y;
		// �õ��û���Ļ��С
		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		x = (size.width - 600) / 2;
		y = (size.height - 400) / 2;
		setSize(600, 400);
		// �ó��������ʾ����Ļ����
		setLocation(x, y);
		// ���˴��ڵ���С��С����Ϊһ������ֵ��
		setMinimumSize(new Dimension(250, 150));
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	// ��ʼ������
	void init() {
		files.setMnemonic('F');
		edit.setMnemonic('E');
		formats.setMnemonic('O');
		help.setMnemonic('H');
		newFile.setMnemonic('N');
		open.setMnemonic('O');
		save.setMnemonic('S');
		saveAs.setMnemonic('A');
		exit.setMnemonic('X');
		undo.setMnemonic('U');
		cut.setMnemonic('T');
		copy.setMnemonic('C');
		paste.setMnemonic('P');
		selectAll.setMnemonic('A');
		timeDate.setMnemonic('D');
		lineWrap.setMnemonic('M');
		about.setMnemonic('A');
		// Ϊ�ؼ�������Ƿ�
		newFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
		open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
		save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
		exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
		cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
		copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
		paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
		selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));
		// Ϊ�ؼ���ӿ�ݼ�
		timeDate.setAccelerator(KeyStroke.getKeyStroke("F5"));

		files.add(newFile);
		files.add(open);
		files.add(save);
		files.add(saveAs);
		files.addSeparator();
		files.add(exit);
		edit.add(undo);
		edit.addSeparator();
		edit.add(cut);
		edit.add(copy);
		edit.add(paste);
		edit.addSeparator();
		edit.add(selectAll);
		edit.add(timeDate);
		formats.add(lineWrap);
		formats.add(fonts);
		help.add(about);

		menub.add(files);
		menub.add(edit);
		menub.add(formats);
		menub.add(help);
		setJMenuBar(menub);
		getContentPane().add(new JScrollPane(text));

		Listen listen = new Listen();
		Listen1 listen1 = new Listen1();
		// Ϊ�ؼ�����¼�������
		newFile.addActionListener(listen);
		undo.addActionListener(listen);
		open.addActionListener(listen);
		save.addActionListener(listen);
		saveAs.addActionListener(listen);
		exit.addActionListener(listen);
		cut.addActionListener(listen);
		copy.addActionListener(listen);
		paste.addActionListener(listen);
		selectAll.addActionListener(listen);
		timeDate.addActionListener(listen);
		lineWrap.addActionListener(listen);
		about.addActionListener(listen);
		open.addActionListener(listen1);
		save.addActionListener(listen1);
		saveAs.addActionListener(listen1);
		// ��ʱû��ʵ�ֵĹ��� :��������
		// undo����û��ʵ��
		fonts.setEnabled(false);

	}

	class Listen implements ActionListener {
		// ʵ������һ��������¼�������
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			if (source == newFile) {
				text.setText("");
				// ���ñ���
				th.setTitle(name);
				openedPath = null;
				opened = false;
			} else if (source == exit)
				setVisible(false);
			else if (source == undo)
				try {
					// �˹���û��ʵ�� ����Ҫ��ջ��
					// undo.setEnabled(undoManager.canUndo());
					undoManager.undo();
				} catch (CannotRedoException cre) {
					cre.printStackTrace();
				}
			else if (source == selectAll)
				text.selectAll();
			else if (source == cut)
				text.cut();
			else if (source == copy)
				text.copy();
			else if (source == paste)
				text.paste();
			else if (source == lineWrap)
				// �����ı����Ļ��в���(��ȡ�ı����Ļ��в���)
				text.setLineWrap(!text.getLineWrap());
			else if (source == about) {
				String message = "--------\n�汾��1.0\n���ߣ�ʱ��" + "\n��������Ҫ�ö�ջ��������" + "\n���������ʽ" + "\n��ʱû��ʵ��" + "\n\n��л����ʹ��";
				JOptionPane.showMessageDialog(th, message, "����", JOptionPane.PLAIN_MESSAGE);
			} else if (source == timeDate) {
				Date nowTime = new Date();
				SimpleDateFormat times = new SimpleDateFormat("HH:mm yyyy-MM-dd");
				text.insert(times.format(nowTime), text.getCaretPosition());
			}
		}
	}

	class Listen1 implements ActionListener {
		// ʵ�����ڶ��ļ����в������¼�������
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			// ���ļ��¼�
			if (source == open) {
				// ��ʾ�Ի����� �Ա�ѡ���ļ�
				FileDialog openFile = new FileDialog(th, "���ļ�", FileDialog.LOAD);
				openFile.setVisible(true);
				// ��ȡ�ļ�·��
				String filePath = openFile.getDirectory() + openFile.getFile();
				try {
					FileInputStream fis = new FileInputStream(filePath);
					byte[] content = new byte[fis.available()];
					fis.read(content);
					text.setText(new String(content));
					// ���� TextComponent ���ı��������λ��
					text.setCaretPosition(0);
					if (openFile.getFile() != null) {
						th.setTitle(openFile.getFile() + name);
						openedPath = filePath;
						opened = true;
					}
					fis.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				opened = true;
			}
			// ���漰���Ϊ�¼�
			else if (source == save || source == saveAs) {
				String savePath = openedPath;
				if (savePath == null || source == saveAs) {
					// ��� mode ��ֵΪ LOAD����ô�ļ��Ի��򽫲���Ҫ��ȡ���ļ�������ʾ���ļ��ǵ�ǰĿ¼�е��ļ�
					// ��� mode ��ֵΪ SAVE�����ļ��Ի��򽫲���Ҫд���ļ���λ�á�
					FileDialog saveFile = new FileDialog(th, "�����ļ�", FileDialog.SAVE);
					saveFile.setVisible(true);
					savePath = saveFile.getDirectory() + saveFile.getFile();
				}
				try {
					FileOutputStream fos = new FileOutputStream(savePath);
					fos.write(text.getText().getBytes());
					fos.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				if (source == save)
					openedPath = savePath;
			}
		}
	}

	public static void main(String[] args) {
		try {
			// ʹ�õ�ǰ�̵߳�����������������ظ�����������ָ���� LookAndFeel
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			System.err.println("Couldn't use the system look and feel:" + e);
		}
		String name = ("--���±� �汾1.0--");
		MyNote note = new MyNote(name);
		note.init();
		note.setVisible(true);
	}
}
